package com.dougmccune.charts.gantt
{
	import mx.core.mx_internal;
	import flash.events.Event;
	import mx.controls.listClasses.BaseListData;
	import mx.controls.listClasses.IListItemRenderer
	import mx.controls.listClasses.IDropInListItemRenderer
	import mx.controls.DataGrid;
	import mx.formatters.NumberFormatter;
	import mx.events.FlexEvent;
	
	use namespace mx_internal;
	
	[ExcludeClass]
	
	public class GanttItemEditor extends GanttSlider implements mx.controls.listClasses.IListItemRenderer, mx.controls.listClasses.IDropInListItemRenderer
	{
		override protected function createChildren():void {
			super.createChildren();
			this.addEventListener(Event.CHANGE, sliderChanged);
		}
		
		private var _data:Object;

		
		private function sliderChanged(event:Event):void {
				
			_data.start = this.values[0];
			
			_data.duration = this.values[2] - this.values[0];
			
			_data.percentComplete = (this.values[1] - this.values[0])/(this.values[2] - this.values[0]);
			_data.max = this.maximum;
			
			var grid:DataGrid = (_listData.owner as DataGrid);
			
			grid.dataProvider[grid.selectedIndex] = _data;
			
			this.data = _data;
		}
		
		[Bindable("dataChange")]
		
		public function set data(value:Object):void {
			_data = value;
			
			var start:Number = _data.start;
			var end:Number = _data.start + _data.duration;
			var middle:Number = _data.start + (end - _data.start)*_data.percentComplete;
			
			this.enabled = _data.selected;
			
			this.values = [start, middle, end];	
			
			this.maximum = (this._listData.owner as GanttDataGrid).max;
			
			
			
			dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
		}
		
		public function get data():Object {
	        return _data;
	    }

		
		
       
       // Internal variable for the property value.
	    private var _listData:BaseListData;
	    
	    // Make the listData property bindable.
	    [Bindable("dataChange")]
	    
	    // Define the getter method.
	    public function get listData():BaseListData
	    {
	      return _listData;
	    }
	    
	    // Define the setter method,
	    public function set listData(value:BaseListData):void
	    {
	      _listData = value;
	  
	    }
	}
}